var express = require('express');
var router = express.Router();
var fs = require('fs');
var path = require('path');
const multer = require('multer')
const redisUtil=require('../utils/redisUtils')
var xlsx=require('node-xlsx')

function formatReq(code, msg, data) {
  return {
    code: code,
    data: data,
    message: msg
  }
}



router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});


router.post('/getFile', multer({
  //设置文件存储路径
  dest: 'public/xmlfile'
}).array('file', 1), function (req, res, next) {
  let files = req.files;
  let file = files[0];

  let fileInfo = {};
  let path = 'public/xmlfile/' + Date.now().toString() + '_' + file.originalname;
  fs.renameSync('./public/xmlfile/' + file.filename, path);
  //获取文件基本信息
  fileInfo.type = file.mimetype;
  fileInfo.name = file.originalname;
  fileInfo.size = file.size;
  fileInfo.path = path;
  console.log(path)
  // var buf = new Buffer.alloc(1024);
  // fs.open('1638432181489_新建 XLSX 工作表.xlsx', 'r+', function(err, fd) {
  //   if (err) {
  //     return console.error(err);
  //   }
  //   console.log("文件打开成功！");
  //   console.log("准备读取文件！");
  //   fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
  //
  //     // 仅输出读取的字节
  //       let obj=xlsx.parse(buf)
  //       redisUtil.set('xmldata',JSON.stringify(obj),3600)
  //
  //
  //     // 关闭文件
  //     fs.close(fd, function(err){
  //       if (err){
  //         console.log(err);
  //       }
  //       console.log("文件关闭成功");
  //     });
  //   });
  // });
  let obj=xlsx.parse(path)
       redisUtil.set('xmldata',JSON.stringify(obj),3600)

});




module.exports = router;
